<?php
include 'includes/application_top.inc';
 
		if(@$_REQUEST['security']=="RAGISTRATION")
		{
		$first_name = $_REQUEST['first_name'];
		$last_name = $_REQUEST['last_name'];
		$email = $_REQUEST['email'];
		$pass = md5($_REQUEST['pass']);
		$today = date("Y-m-d H:i:s"); 
		
		/*$_SESSION['first_name'] = $first_name;
		$_SESSION['last_name'] = $last_name;
		$_SESSION['email'] = $email;
		 
		      $sql = "Select * from url_users Where email='$email'";
			  //"SELECT * FROM `url_users` WHERE `email` = '$email'"
					$resultget = mysql_query($sql);
				    echo $nums = mysql_num_rows($resultget);//die();
					
						if($nums>0)
						{
						echo	$successUrl1 = ROOTURL.'register.php?ac=ermsg';
						    header("Location: $successUrl1");
							die();
						}else
						{
							echo $nums;die();
						}
						*/
						
			
			try {
				   	if(trim($_REQUEST['pass'])==trim($_REQUEST['confirmPass']))
							{
								$query = "INSERT INTO url_users(u_id,first_name,last_name,email,password,create_datetime) values(NULL,'$first_name','$last_name','$email','$pass','$today')";
								$result = mysql_query($query);// or die ("Error in query: $query " . mysql_error());
								$insert_id = mysql_insert_id();
							}
							
							if( $insert_id=="" &&  $insert_id==0)
							{
								$error = "Error in query: $query " . mysql_error();
							    throw new Exception($error);
							}
					   
				} catch (Exception $e) {
					echo '<br>Caught exception: ',  $e->getMessage(), "\n";
				}
				 
					$successUrl = ROOTURL.'register.php?ac=msg';
					header("Location: $successUrl");
					//header("Location: http://www.example.com/");
		}



			if(@$_REQUEST['security']=="SHORTURL")
			{
					/*$url = $_REQUEST['url'];
			        $checkurl = checkvalidUrl($url);
					
		         if($checkurl!="wrongurl")
					{
						   echo "worng";
						   echo $notsuccessUrl = ROOTURL.'listing_grid.php?ac=emsg';				 
							//print_r($_REQUEST);
							//die();
					        header("Location: $notsuccessUrl");echo "<pre>";
					}else
						{*/
						//echo "Right";
							$str = @$_REQUEST['url'];
							$str = preg_replace('#^https?://#', '', $str);
							//echo 
							$url = "http://".$str;
					 try {	 
							 if(@$_REQUEST['url'] !="")
							 {
							 	   $url = "http://".$str;				 
					              // $url = checkvalidUrl($url);
								 $sql  = "SELECT id FROM  `shorturl` ORDER BY id DESC LIMIT 0 , 1";
								  $quer= mysql_query($sql);
								  $num = mysql_fetch_array($quer);
								  $charlen = 6 - strlen($num['id']);
								  $string= $num['id'].makeID($charlen);
								  $today = date("Y-m-d H:i:s"); 
								  $u_id = $_SESSION['u_id'];
								  $query = "INSERT INTO shorturl(id,u_id,shorturl_code,long_url,create_datetime) values(NULL,'$u_id','$string','$url','$today')";
							 
								  $result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
								  $insert_id = mysql_insert_id();
								//   echo "<h1>Success!</h1>";
								//   $shorturl = ROOTURL.$string;
								//echo '<p>Your short URL is <a  target="_blank" href="'.$shorturl.'">'.$shorturl.'</a></p>';
								  $successUrl = ROOTURL.'listing_grid.php?ac=suss';header("Location: $successUrl");
					//	print_r($_REQUEST);
					//	die();
					
							 }
					} catch (Exception $e) {
						echo '<br>Caught exception: ',  $e->getMessage(), "\n";
					}
					mysql_close($con);
			     }
				 //print_r($_REQUEST);
			      if(@$_REQUEST['status']=="DELETESHURL")
			      {
			      	
					//print_r($_REQUEST);
			      	//die();
					//exit();
					
			      	$id = $_REQUEST['id'];	
					$tpages = $_REQUEST['tpages'];	
					$page = $_REQUEST['page'];		
					$sql = "DELETE FROM `ttsl_urlredirectiongateway`.`shorturl` WHERE `shorturl`.`id` =".$id;
					mysql_query($sql);		
				 	$successUrl = ROOTURL.'listing_grid.php?tpages='.$tpages.'&page='.$page.'&acd=dsuss';header("Location: $successUrl");
			      }
			      
			       if(@$_REQUEST['EditSurl']=="EDITSHURL" && @$_REQUEST['status']=="EDITSHURL")
			      {
			      	$id = $_REQUEST['id'];	
					$tpages = $_REQUEST['tpages'];	
					$page = $_REQUEST['page'];		
					$url = $_REQUEST['url'];
					//$sql = "DELETE FROM `ttsl_urlredirectiongateway`.`shorturl` WHERE `shorturl`.`id` =".$id;
					echo $sql = "UPDATE shorturl SET long_url = '".$url."' WHERE id =".$id;
					mysql_query($sql);		
					$successUrl = ROOTURL.'listing_grid.php?tpages='.$tpages.'&page='.$page.'&aced=edsuss'.'&Msg=Successfully Updated';header("Location: $successUrl");
			      }

?>